﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HotelCode.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PhotoController : ControllerBase
    {
        [HttpPost]
        public IActionResult GetPhoto(IFormFile file)
        {
            try
            {
                string newfile = Directory.GetCurrentDirectory() + @"\wwwroot\imgs\" + file.FileName;
               // var t = file.contenttype;
               // var ts = new list<string>
               //{
               //    "imgs/png",
               //    "imgs/jpeg",
               //    "imgs/webep",
               //};
               // if (!ts.contains(t))
               // {
               //     return ok("不是图片");
               // }
               // var m = 1024 * 1024 * 2;
               // if(file.length>m)
               // {
               //     return ok("大于2m");
               // }
                using (FileStream fs=new FileStream(newfile,FileMode.Create))
                {
                    file.CopyTo(fs);
                    fs.Close();
                }
                return Ok("https://localhost:7163/imgs/"+file.FileName);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
